package com.sugar.real.estate.module.business.controller.admin.designplan;

import com.sugar.real.estate.framework.apilog.core.annotation.ApiAccessLog;
import com.sugar.real.estate.framework.common.pojo.CommonResult;
import com.sugar.real.estate.framework.common.pojo.PageParam;
import com.sugar.real.estate.framework.common.pojo.PageResult;
import com.sugar.real.estate.framework.common.util.object.BeanUtils;
import com.sugar.real.estate.framework.excel.core.util.ExcelUtils;
import com.sugar.real.estate.module.business.controller.admin.designplan.vo.DesignPlanPageReqVO;
import com.sugar.real.estate.module.business.controller.admin.designplan.vo.DesignPlanRespVO;
import com.sugar.real.estate.module.business.controller.admin.designplan.vo.DesignPlanSaveReqVO;
import com.sugar.real.estate.module.business.controller.admin.designplan.vo.DesignPlanSimpleRespVO;
import com.sugar.real.estate.module.business.dal.dataobject.designplan.DesignPlanDO;
import com.sugar.real.estate.module.business.service.designplan.DesignPlanService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static com.sugar.real.estate.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static com.sugar.real.estate.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 设计方案")
@RestController
@RequestMapping("/business/design-plan")
@Validated
public class DesignPlanController {

    @Resource
    private DesignPlanService designPlanService;

    @PostMapping("/create")
    @Operation(summary = "创建设计方案")
    @PreAuthorize("@ss.hasPermission('business:design-plan:create')")
    public CommonResult<Long> createDesignPlan(@Valid @RequestBody DesignPlanSaveReqVO createReqVO) {
        return success(designPlanService.createDesignPlan(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新设计方案")
    @PreAuthorize("@ss.hasPermission('business:design-plan:update')")
    public CommonResult<Boolean> updateDesignPlan(@Valid @RequestBody DesignPlanSaveReqVO updateReqVO) {
        designPlanService.updateDesignPlan(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除设计方案")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:design-plan:delete')")
    public CommonResult<Boolean> deleteDesignPlan(@RequestParam("id") Long id) {
        designPlanService.deleteDesignPlan(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除设计方案")
                @PreAuthorize("@ss.hasPermission('business:design-plan:delete')")
    public CommonResult<Boolean> deleteDesignPlanList(@RequestParam("ids") List<Long> ids) {
        designPlanService.deleteDesignPlanListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得设计方案")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:design-plan:query')")
    public CommonResult<DesignPlanRespVO> getDesignPlan(@RequestParam("id") Long id) {
        DesignPlanDO designPlan = designPlanService.getDesignPlan(id);
        return success(BeanUtils.toBean(designPlan, DesignPlanRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得所有设计方案")
    public CommonResult<List<DesignPlanSimpleRespVO>> getDesignPlanAll() {
        return success(designPlanService.getDesignPlanAll());
    }

    @GetMapping("/page")
    @Operation(summary = "获得设计方案分页")
    @PreAuthorize("@ss.hasPermission('business:design-plan:query')")
    public CommonResult<PageResult<DesignPlanRespVO>> getDesignPlanPage(@Valid DesignPlanPageReqVO pageReqVO) {
        PageResult<DesignPlanDO> pageResult = designPlanService.getDesignPlanPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DesignPlanRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出设计方案 Excel")
    @PreAuthorize("@ss.hasPermission('business:design-plan:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDesignPlanExcel(@Valid DesignPlanPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DesignPlanDO> list = designPlanService.getDesignPlanPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "设计方案.xls", "数据", DesignPlanRespVO.class,
                        BeanUtils.toBean(list, DesignPlanRespVO.class));
    }

}